//
//  ViewController.m
//  轮播图无限循环
//
//  Created by mac on 2017/4/1.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"

#define Number 7

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height - 50 - 100

//#define WIDTH WIDTH - 20 * 2
//#define HEIGHT HEIGHT - 50 * 2

@interface ViewController ()<UIScrollViewDelegate>
{
    // 左边图片视图
    UIImageView * _leftImageView;
    // 中间图片视图
    UIImageView * _centerImageView;
    // 右边的图片视图
    UIImageView * _rightImageView;
    // 存放图片的数据源
    NSMutableArray * _imageArray;
    // 记录中间视图显示的图片位于图片数组中的位置
    NSInteger currentImageIndex;
    
    UIScrollView *_browseScrollView;
    
    UIPageControl *_pageControl;
    
    NSTimer *_timer;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self createDataSource];
    
    [self createScrollView];
    
    [self createPageControl];
    
    [self addTimer];

}

#pragma mark -
#pragma mark Timer 方法
- (void)addTimer{
    //方法一：滚动scrollView 计时器依然在运行
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
//    添加定时器，需要将它添加到NSRunLoopCommonModes状态才能在scroll滚动的时候不受影响，常用于tableView或CollectionView中有轮播图的情况
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
//    子线程的情况下需要自己run，主线程不需要这行代码
//    [[NSRunLoop currentRunLoop] run];
    
    //方法二：滚动scrollView 计时器暂停
//    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];

}

- (void)run{

    
    [self scrollViewDidEndDecelerating:_browseScrollView];
}

- (void)createDataSource{

    _imageArray = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 7; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
        [_imageArray addObject:image];
    }
    //得到 1，2，3，4，5，6，7
    
    //取出数组中的第一张，插到数组第0个的位置
    UIImage *imag = [_imageArray lastObject];
    [_imageArray removeLastObject];
    [_imageArray insertObject:imag atIndex:0];
    
    //得到 7，1，2，3，4，5，6
}

- (void)createScrollView{

    _browseScrollView = [[UIScrollView alloc] init];
    _browseScrollView.backgroundColor = [UIColor greenColor];
    _browseScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
    _browseScrollView.pagingEnabled = YES;
    _browseScrollView.showsHorizontalScrollIndicator = NO;
    _browseScrollView.delegate = self;
    [self.view addSubview:_browseScrollView];

    [_browseScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(@50);
        make.width.mas_equalTo(@(WIDTH - 20 * 2));
        make.height.mas_equalTo(@(HEIGHT));
        
        
    }];
    
    //添加 左、中、右  三个相框
    //左
    _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _leftImageView.image = _imageArray[0];
    [_browseScrollView addSubview:_leftImageView];
    
    //中
    _centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    _centerImageView.image = _imageArray[1];
    [_browseScrollView addSubview:_centerImageView];
    
    //右
    _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT)];
    _rightImageView.image = _imageArray[2];
    [_browseScrollView addSubview:_rightImageView];
    
    //设置scrollView的偏移量，只显示中间的那张
    [_browseScrollView setContentOffset:CGPointMake(WIDTH, 0) animated:NO];
    //最初 左：7， 中：1， 右：2
    currentImageIndex = 1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

//    CGFloat x = scrollView.contentOffset.x;
//    NSLog(@"偏移量：%lf", x);
    CGFloat x = (WIDTH - 20 * 2) * 2;
    
    //显示左：偏移量 0； 显示中间：偏移量 375； 显示右边：偏移量 750
    //获取当前显示的图片在数组中的下标
    //当x=0 ->图片右移,需要从最右边取出图片放到最左边接应
    if (x == 0) {
        currentImageIndex = (currentImageIndex - 1 + _imageArray.count) % _imageArray.count;
    }
    
    //当x=WIDTH * 2 -》图片左移，需要从最左边取出图片放到最右边接应
    if (x == (WIDTH - 20 * 2) * 2) {
        currentImageIndex = (currentImageIndex + 1) % _imageArray.count;
    }
    
    //中相框显示
    _centerImageView.image = _imageArray[currentImageIndex];
    
    //左相框显示
    _leftImageView.image = _imageArray[(currentImageIndex - 1 + _imageArray.count) % _imageArray.count];

    //右相框显示
    _rightImageView.image = _imageArray[(currentImageIndex + 1) % _imageArray.count];
    
    //滚动视图偏移量 一直显示中间视图-》复位
    [scrollView setContentOffset:CGPointMake(WIDTH, 0) animated:NO];
    
       static int page = 0;
        if (page < 6) {
            page ++;
        }else{
            page = 0;
        }
    _pageControl.currentPage = page;
}

-(void)createPageControl{
    //初始化UIPageControl
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(40, [UIScreen mainScreen].bounds.size.height - 100, WIDTH - 80, 40)];
    //    设置背景色
//    _pageControl.backgroundColor = [UIColor lightGrayColor];
    //    总共有多少页
    _pageControl.numberOfPages = Number;
    //    设置当前页
    // default is 0.
    _pageControl.currentPage = 0;
    
    _pageControl.pageIndicatorTintColor = [UIColor blackColor];
    
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    [_pageControl addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    
    //    添加到视图
    [self.view addSubview:_pageControl];
}


-(void)valueChange:(UIPageControl*)pageControl{
    
//    int page = 0;
//    if (page < 7) {
//        page ++;
//    }else{
//        page = 0;
//    }
//    
//    [_browseScrollView setContentOffset:CGPointMake(page * WIDTH, 0) animated:YES];
}

////开始拖拽的时候停止定时器
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//
//    [self removeTimer];
//}
//
////结束拖拽再重启定时器
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [self addTimer];
//}
//
//- (void)removeTimer{
//    [_timer invalidate];
//    _timer = nil;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
